<?php

setlocale(LC_ALL, "ru_RU.UTF-8");
mb_internal_encoding("UTF-8");

$curRealDir = mb_substr($_SERVER["SCRIPT_FILENAME"], 0, mb_strrpos($_SERVER["SCRIPT_FILENAME"], "/"));

require_once ("./config/setup.php");
require_once ("./libs/db.class.php");
require_once ("./libs/utils.inc.php");
$sm = new SSmarty();
$db = new DB();

//--правый блок
//--из админки
$LAST_TAGS_COUNT = getOption($db, "LAST_TAGS_COUNT");
//-------------------

$db->dbq("
            Select
                tags
            From
                news
        ");
$lastTags = array();
while ($arr = $db->row()) {
    $tags = json_decode($arr[0], true);
    foreach ($tags as $key => $tag) {
        if ($tag != "") {
            if (!isset($lastTags[$tag])) {
                $query = preg_split("'
                                        [\ ,.\t\r\n]+
                                    'iusx", $tag);
                $utf8BytesRegexp = array();
                foreach ($query as $key => $value) {
                    $utf8BytesRegexp[] = mb_substr(json_encode($value), 1, mb_strlen(json_encode($value)) - 2);
                }
                $utf8BytesRegexp = implode("(.*)", $utf8BytesRegexp);
                $utf8BytesRegexp = addslashes(addslashes($utf8BytesRegexp));
                $regexp = implode("(.*)", $query);
                
                $db->dbqi(1, "
                            Select
                                Count(*)
                            From
                                news
                            Where
                                caption Regexp(\"" . $regexp . "\") ||
                                maintext Regexp(\"" . $regexp . "\") ||
                                DATE_FORMAT(postdate, \"%d %M %Y, %H:%i\") Regexp(\"" . $regexp . "\") ||
                                author Regexp(\"" . $regexp . "\") ||
                                tags Regexp(\"" . $utf8BytesRegexp . "\")
                        ");
                $arr = $db->rowi(1);
                $lastTags[$tag] = $arr[0];
            }
        }
    }
}

arsort($lastTags);
$lastTags = array_splice($lastTags, 0, $LAST_TAGS_COUNT);
$sm->assign("lastTags", $lastTags);

//------------------------------------

$sm->assign("design", "http://" . DOMEN_NAME . "/" . $sm->template_dir);
$sm->assign("displayedHtml", "about.html");
$sm->display("about.html");
?>
